package com.changgou.controller;


import com.changgou.common.entity.Page;
import com.changgou.service.ESManagerService;
import com.changgou.service.SearchService;
import com.chaunggou.search.pojo.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private ESManagerService esManagerService;

    @Autowired
    private SearchService searchService;

    /**
     * 搜索页面
     * 参数 : Map
     * 返回值 : Map
     * 由于页面是thymeleaf 完成的 属于服务器内页面渲染 跳转页面
     *
     */
    @GetMapping("/list")
    public String search(@RequestParam Map<String,String> searchMap, Model model)throws Exception{
        //特殊符号处理
        handlerSearchMap(searchMap);

        //执行查询返回值
        Map<String,Object> resultMap = searchService.search(searchMap);

        //搜索条件
        model.addAttribute("searchMap",searchMap);
        //搜索数据结果
        model.addAttribute("result",resultMap);

        /*
        封装分页数据并返回
        总记录数
        当前页
        每页显示条数 (默认值显示20条)
         */
        Page<SkuInfo> page = new Page<>(
                Long.parseLong(String.valueOf(resultMap.get("total"))),
                Integer.parseInt(String.valueOf(resultMap.get("pageNum"))),
                Page.pageSize
        );

        model.addAttribute("page",page);
        //拼装URI
        StringBuilder url = new StringBuilder("/search/list");
        if (searchMap != null && searchMap.size() > 0){
            //查询条件
            url.append("?");
            for (String paramKey : searchMap.keySet()) {
                if (!"sortRule".equals(paramKey) && !"sortField".equals(paramKey) && !"pageNum".equals(paramKey)){
                    //拼装url路径
                    url.append(paramKey).append("=").append(searchMap.get(paramKey)).append("&");
                }
            }
            //转为 String 类型
            String urlString = url.toString();
            //去除url路径上最后一个 & 符号
            urlString   = urlString.substring(0, urlString.length() - 1);
            model.addAttribute("url",urlString);
        }else {
            model.addAttribute("url",url);
        }
        return "search";
    }



    /**
     * 全文检索
     */

    @GetMapping
    @ResponseBody
    public Map search(@RequestParam Map<String,String> paramMap) throws  Exception{
        //特殊符号处理
        this.handlerSearchMap(paramMap);
        Map search = searchService.search(paramMap);
        return search;
    }

    //对搜索入参带有特殊符号进行处理
    public void handlerSearchMap(Map<String,String> stringMap){
        //判断不为空
        if (stringMap != null){

            //映射 stringMap
            Set<Map.Entry<String,String>> entrySet = stringMap.entrySet();

            //遍历
            for (Map.Entry<String, String> entry : entrySet) {
                if (entry.getKey().startsWith("spec_")){
                    stringMap.put(entry.getKey(),entry.getValue().replace("+","%2B"));
                }
            }
        }
    }


 }
